package com.oocl.im.handler.impl;

import com.oocl.im.constants.ServerConstants;
import com.oocl.im.entity.Client;
import com.oocl.im.constants.ServerMap;
import com.oocl.im.entity.Protocol;
import com.oocl.im.handler.IHandle;
import com.oocl.im.outputstream.impl.NetOutput;

import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by WUJO5 on 7/15/2017.
 */
public class ShakeHandle implements IHandle {
    ObjectOutputStream oos;

    public ObjectOutputStream getOos() {
        return oos;
    }

    public void setOos(ObjectOutputStream oos) {
        this.oos = oos;
    }

    public ShakeHandle(ObjectOutputStream oos) {

        this.oos = oos;
    }

    @Override
    public void handle(Client client, Protocol protocol) {
        System.out.println("here is in shake handle");
        Client to = (Client) protocol.getParams().get(ServerConstants.TO);
        System.out.println(to.getAccount());
        if ("@all".equals((String)to.getAccount()))
        {
            protocol.setMsgType(ServerConstants.SHAKE);
            Iterator iterator = ServerMap.socketMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                new NetOutput((ObjectOutputStream) entry.getValue()).write(protocol);
            }
        }else {
            protocol.setMsgType(ServerConstants.SHAKE);
            ObjectOutputStream toOos = ServerMap.socketMap.get(to);
            System.out.println("shake:"+protocol);
            try {

                NetOutput tmpOut = new NetOutput(toOos);
                tmpOut.write(protocol);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
